package cn.wu.jishupeizhi.rabbitmq;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Fanout {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    final String exchange = "simple.Exchange";
    final String queueName1 = "simple.queue1";
    final String queueName2 = "simple.queue2";
    final String message = "hello,every one!";

    //生产者
    @Test
    public void pushRabbitMq1() throws Exception {
        rabbitTemplate.convertAndSend(exchange,"red", message);
    }

    //消费者
    @RabbitListener(bindings = @QueueBinding(
            //队列
            value = @Queue(name = queueName1),
            //交换机
            exchange = @Exchange(name = exchange, type = ExchangeTypes.FANOUT)
    ))
    public void pullRabbitMq1(String message) {
        System.out.println("queue1:" + message);
    }

    //消费者
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = queueName2),
            exchange = @Exchange(name = exchange, type = ExchangeTypes.FANOUT)
    ))
    public void pullRabbitMq2(String message) {
        System.out.println("queue1:" + message);
    }
}
